function normalizeLink(to, location = history.location) {
    to = to || '';

    if (to && to[0] === '#') {
        to = location.pathname + location.search + to;
    } else if (to && to[0] === '?') {
        to = location.pathname + to;
    }

    const idx = to.indexOf('?');
    const idx2 = to.indexOf('#');
    let pathname = ~idx
        ? to.substring(0, idx)
        : ~idx2
            ? to.substring(0, idx2)
            : to;
    let search = ~idx ? to.substring(idx, ~idx2 ? idx2 : undefined) : '';
    let hash = ~idx2 ? to.substring(idx2) : location.hash;

    if (!pathname) {
        pathname = location.pathname;
    } else if (pathname[0] != '/' && !/^https?\:\/\//.test(pathname)) {
        let relativeBase = location.pathname;
        const paths = relativeBase.split('/');
        paths.pop();
        let m;
        while ((m = /^\.\.?\//.exec(pathname))) {
            if (m[0] === '../') {
                paths.pop();
            }
            pathname = pathname.substring(m[0].length);
        }
        pathname = paths.concat(pathname).join('/');
    }
    return pathname + search + hash;
}

//获取当前路径
function getHeaderpaths($pages = app.pages,url =normalizeLink(),$preUrl){
    if(!url){
        return "1";
    }

    $preUrl = $preUrl||'/';
    if( $preUrl.length>0 && $preUrl[0] !== '/'){
        $preUrl = "/"+$preUrl;
    }
    var result = "false";
    for(var i in $pages){
        var $preurl2 = $preUrl;
        if($pages[i].url  && $pages[i].url.length>0 ){
            var $after = $pages[i].url;
            if($after[0] !== '/'){
                $after ="/"+$after;
            }
            $preurl2 = $after;
            if($preUrl !== "/"){
                $preurl2 = $preUrl+$after;
            }
        }

        if( $preurl2 === url){
            if($pages[i].hasOwnProperty("headerIndex")){
                return $pages[i].headerIndex;
            }
            if($preUrl === "/"){
                return i+1;
            }else{
                return "back"
            }
        }
        if($pages[i].children) {
            result = getHeaderpaths($pages[i].children, url, $preurl2);
            if (result !== "false") {
                return result;
            }
        }
    }
    return "false";
}


function isCurrentUrl(to, ctx) {
    if (!to) {
        return false;
    }
    const pathname = history.location.pathname;
    const link = normalizeLink(to, {
        ...location,
        pathname,
        hash: ''
    });

    if (!~link.indexOf('http') && ~link.indexOf(':')) {
        let strict = ctx && ctx.strict;
        return match(link, {
            decode: decodeURIComponent,
            strict: typeof strict !== 'undefined' ? strict : true
        })(pathname);
    }

    return decodeURI(pathname) === link;
}



//上划设置透明
var setOpacity = function(el,i){
    if(window.getComputedStyle){// for non-IE
        el.style.opacity = i;
    }else if(document.documentElement.currentStyle){ // for IE
        if(!el.currentStyle.hasLayout){
            el.style.zoom = 1;
        }
        if(!el.currentStyle.hasLayout){ //在IE8中zoom不生效，所以再次设置inline-block
            el.style.display = 'inline-block';
        }
        try{
            //测试是否已有filter
            //http://msdn.microsoft.com/en-us/library/ms532847%28VS.85%29.aspx
            if(el.filters){
                if(el.filters('alpha')){
                    el.filters('alpha').opacity = i * 100;
                }else{
                    el.style.filter += 'alpha(opacity='+ i * 100 +')';
                }
            }
        }catch(e){
            el.style.filter = 'alpha(opacity='+ i * 100 +')';
        }
    }
}
